﻿@model CustomerBirthdayModel
@{
    var gridPageSize = EngineContext.Current.Resolve<Nop.Core.Domain.Common.AdminAreaSettings>().GridPageSize;
}

@using Telerik.Web.Mvc.UI

<table width="100%">
    <tr>
        <td class="adminTitle">
            @Html.NopLabelFor(model => model.MonthOfBirthday):
        </td>
        <td class="adminData">
            <select class="form-control" id="monthId">
                <option value="0">All</option>
                @for (int i = 1; i <= 12; i++)
                {
                    <text>
                        <option value="@i">@i</option>
                    </text>
                }
            </select>
        </td>
    </tr>
    <tr>
        <td class="adminTitle">
            @Html.NopLabelFor(model => model.SearchCustomerRoleIds):
        </td>
        <td class="adminData">
            @foreach (var customerRole in Model.AvailableCustomerRoles)
            {
                <input type="checkbox" name="SearchCustomerRoleIds" value="@customerRole.Id"
                       @if (Model.SearchCustomerRoleIds != null && Model.SearchCustomerRoleIds.Contains(customerRole.Id)) { <text> checked="checked" </text>             } />@customerRole.Name
                <br />
            }
        </td>
    </tr>
    <tr>
        <td colspan="2">
            <input type="button" id="search-customer-birthday" class="t-button" value="@T(" admin.customers.reports.runreport")" />
        </td>
    </tr>
</table>
<p>
</p>
<table class="adminContent">
    <tr>
        <td>
            @(Html.Telerik().Grid<CustomerModel>()
                  .ClientEvents(events => events.OnDataBinding("onDataBinding"))
                  .Name("customers-birthday-grid")
                  .Columns(columns =>
                  {
                      columns.Bound(x => x.Id);
                      columns.Bound(x => x.Email);
                      columns.Bound(x => x.FullName);
                      columns.Bound(x => x.CustomerRoleNames);
                      columns.Bound(x => x.CellPhone);
                      columns.Bound(x => x.Phone);
                      columns.Bound(x => x.ZipPostalCode);
                      columns.Bound(x => x.Active);
                      columns.Bound(x => x.CreatedOn);
                      columns.Bound(x => x.LastActivityDate);
                      columns.Bound(x => x.DateOfBirth);
                  })
                  .Pageable(settings => settings.PageSize(gridPageSize).Position(GridPagerPosition.Both))
                  .DataBinding(dataBinding => dataBinding.Ajax().Select("ReportCustomerBirthdays", "Customer"))
                  .Sortable()
                  .Filterable()
                  .EnableCustomBinding(true))
        </td>
    </tr>
</table>

<script type="text/javascript">

    $(function () {
        $('#search-customer-birthday').click(function () {
            var grid = $('#customers-birthday-grid').data('tGrid');
            grid.currentPage = 1; //new search. Set page size to 1
            grid.ajaxRequest();
            return false;
        });
        $('#monthId').change(function () {
            var grid = $('#customers-birthday-grid').data('tGrid');
            grid.currentPage = 1; //new search. Set page size to 1
        });
    });

    function onDataBinding(e) {
        var customerRoleIds = [];
        $('[name=SearchCustomerRoleIds]:checked').each(function () {
            customerRoleIds.push($(this).val());
        });
        e.data = { SearchCustomerRoleIds: customerRoleIds.join(","), searchMonthOfBirth: $('#monthId').val() };
    }
</script>

